using System;
using System.Xml.Serialization;
using System.IO;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;


namespace BumbleGame
{
    class Results
    {
        private AnimatedTexture NextButton, TownButton, Garden;
        private AnimatedTexture[] yellowFlowers, redFlowers, blueFlowers, orangeFlowers, greenFlowers, purpleFlowers;
        private List<AnimatedTexture> miniFlowers;
        public int visibleFlowers;
        private float time;
        public AnimatedTexture ScoreBoard;
        public int yellow, red, blue, orange, green, purple;
        
        public bool readyForButtons, onNextButton;

        public Results()
        {
            onNextButton = true;
            readyForButtons = false;
            yellow = 0;
            red = 0;
            blue = 0;
            orange = 0;
            green = 0;
            purple = 0;
            miniFlowers = new List<AnimatedTexture>();
            visibleFlowers = 0;
            time = 0.0f;
        }

        public void Load(GraphicsDeviceManager graphics, ContentManager content, Viewport viewport, FlowerTotals flowerTotals)
        {
            onNextButton = true;
            readyForButtons = false;
            yellow = 0;
            red = 0;
            blue = 0;
            orange = 0;
            green = 0;
            purple = 0;
            visibleFlowers = 0;
            miniFlowers.Clear();
            #region load mini flowers
            //random numbers to set the positions of the mini flowers
            Random randX = new Random(flowerTotals.purple + 100); //using totals as seed will hopefully give it a little variation each time
            Random randY = new Random(flowerTotals.orange);

            //make arrays the size of the flower totals for each color, and create new textures for each flower in the array
            yellowFlowers = new AnimatedTexture[flowerTotals.yellow];
            for (int i = 0; i < yellowFlowers.Length; i++)
            {
                yellowFlowers[i] = new AnimatedTexture();
                yellowFlowers[i].Load(graphics.GraphicsDevice, content, "Images/results_yellow", 1, 1, 1, 0, 0, false);
                yellowFlowers[i].SpritePosition = new Vector2(randX.Next(100, 924), randY.Next(100, 668));
            }
            redFlowers = new AnimatedTexture[flowerTotals.red];
            for (int i = 0; i < redFlowers.Length; i++)
            {
                redFlowers[i] = new AnimatedTexture();
                redFlowers[i].Load(graphics.GraphicsDevice, content, "Images/results_red", 1, 1, 1, 0, 0, false);
                redFlowers[i].SpritePosition = new Vector2(randX.Next(100, 924), randY.Next(100, 668));
            }
            blueFlowers = new AnimatedTexture[flowerTotals.blue];
            for (int i = 0; i < blueFlowers.Length; i++)
            {
                blueFlowers[i] = new AnimatedTexture();
                blueFlowers[i].Load(graphics.GraphicsDevice, content, "Images/results_blue", 1, 1, 1, 0, 0, false);
                blueFlowers[i].SpritePosition = new Vector2(randX.Next(100, 924), randY.Next(100, 668));
            }
            orangeFlowers = new AnimatedTexture[flowerTotals.orange];
            for (int i = 0; i < orangeFlowers.Length; i++)
            {
                orangeFlowers[i] = new AnimatedTexture();
                orangeFlowers[i].Load(graphics.GraphicsDevice, content, "Images/results_orange", 1, 1, 1, 0, 0, false);
                orangeFlowers[i].SpritePosition = new Vector2(randX.Next(100, 924), randY.Next(100, 668));
            }
            greenFlowers = new AnimatedTexture[flowerTotals.green];
            for (int i = 0; i < greenFlowers.Length; i++)
            {
                greenFlowers[i] = new AnimatedTexture();
                greenFlowers[i].Load(graphics.GraphicsDevice, content, "Images/results_green", 1, 1, 1, 0, 0, false);
                greenFlowers[i].SpritePosition = new Vector2(randX.Next(100, 924), randY.Next(100, 668));
            }
            purpleFlowers = new AnimatedTexture[flowerTotals.purple];
            for (int i = 0; i < purpleFlowers.Length; i++)
            {
                purpleFlowers[i] = new AnimatedTexture();
                purpleFlowers[i].Load(graphics.GraphicsDevice, content, "Images/results_purple", 1, 1, 1, 0, 0, false);
                purpleFlowers[i].SpritePosition = new Vector2(randX.Next(100, 924), randY.Next(100, 668));
            }
            // now add them to one master list, but mix up the order so its not all one color at a time
            for (int i = 0; i < 2000; i++)
            {
                if (i <= yellowFlowers.Length - 1)
                { miniFlowers.Add(yellowFlowers[i]); }
                if (i <= redFlowers.Length - 1)
                { miniFlowers.Add(redFlowers[i]); }
                if (i <= blueFlowers.Length - 1)
                { miniFlowers.Add(blueFlowers[i]); }
                if (i <= orangeFlowers.Length - 1)
                { miniFlowers.Add(orangeFlowers[i]); }
                if (i <= greenFlowers.Length - 1)
                { miniFlowers.Add(greenFlowers[i]); }
                if (i <= purpleFlowers.Length - 1)
                { miniFlowers.Add(purpleFlowers[i]); }

                if (i > yellowFlowers.Length && i > redFlowers.Length && i > blueFlowers.Length
                    && i > orangeFlowers.Length && i > greenFlowers.Length && i > purpleFlowers.Length)
                { break; }
            }
            #endregion load mini flowers
            //load results screen png's
            Garden = new AnimatedTexture();
            ScoreBoard = new AnimatedTexture();
            NextButton = new AnimatedTexture();
            TownButton = new AnimatedTexture();

            Garden.Load(graphics.GraphicsDevice, content, "Images/results_background", 1, 1, 1, 0, 0, false);
            ScoreBoard.Load(graphics.GraphicsDevice, content, "Images/results", 1, 1, 1, 0, 0, false);
            NextButton.Load(graphics.GraphicsDevice, content, "Images/results_next_select", 1, 1, 1, 0, 0, false);
            TownButton.Load(graphics.GraphicsDevice, content, "Images/results_town_select", 1, 1, 1, 0, 0, false);

            ScoreBoard.SpritePosition = new Vector2(viewport.Width / 2, viewport.Height / 2);
            NextButton.SpritePosition = ScoreBoard.SpritePosition;
            TownButton.SpritePosition = ScoreBoard.SpritePosition;
            Garden.SpritePosition = ScoreBoard.SpritePosition;

            //start scoreboard minimized so it expands to its intended size
            ScoreBoard.SpriteScale = 0;
        }

        public void Update(ref FlowerTotals flowerTotals, float elapsed)
        {

            Garden.UpdateFrame(elapsed);
            //random flower crap, all update
            foreach (AnimatedTexture flower in miniFlowers)
            { flower.UpdateFrame(elapsed); }
            // index how many should be drawn over time
            time += elapsed;
            
            //if all the flowers are visible, do the scoreboard thing, otherwise, keep updating the flowers
            if (visibleFlowers == (flowerTotals.yellow + flowerTotals.red + flowerTotals.blue + flowerTotals.orange + flowerTotals.green + flowerTotals.purple))
            {
                if (time > 0.5f)
                {
                    //bring up scoreboard
                    if (ScoreBoard.SpriteScale < 1.0f) ScoreBoard.SpriteScale += .01f;
                    if (ScoreBoard.SpriteScale > 1.0f) ScoreBoard.SpriteScale = 1.0f;
                    //tally up flower #'s on scoreboard
                    if (ScoreBoard.SpriteScale == 1.0f && !readyForButtons)
                    {
                        if (yellow < flowerTotals.yellow) yellow++;
                        if (red < flowerTotals.red) red++;
                        if (blue < flowerTotals.blue) blue++;
                        if (orange < flowerTotals.orange) orange++;
                        if (purple < flowerTotals.purple) purple++;
                        if (green < flowerTotals.green) green++;

                        if (yellow == flowerTotals.yellow && red == flowerTotals.red && blue == flowerTotals.blue &&
                            orange == flowerTotals.orange && purple == flowerTotals.purple && green == flowerTotals.green)
                        {
                            readyForButtons = true;
                        }
                    }
                }
            }
            else
            {
                if (time > 0.05f)
                {
                    visibleFlowers++;
                    time = 0.0f;
                }
            }

            
            //update button selection
            //any animation
            ScoreBoard.UpdateFrame(elapsed);
            NextButton.UpdateFrame(elapsed);
            TownButton.UpdateFrame(elapsed);
        }

        public void DrawGarden(SpriteBatch batch)
        {
            Garden.DrawFrame(batch);
            //draw mini flowers one at a time
            for (int i = 0; i < visibleFlowers; i++)
            { miniFlowers[i].DrawFrame(batch); }
        }
        public void DrawScore(SpriteBatch batch)
        {
            //draw scoreboard stuff once teh flowers have populated the screen
            ScoreBoard.DrawFrame(batch);
            if (readyForButtons)
            {
                if (onNextButton) NextButton.DrawFrame(batch);
                else TownButton.DrawFrame(batch);
            }
        }

    }
}
